package com.gxa.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.commons.R;
import com.gxa.finance.mapper.ServiceManagementMapper;
import com.gxa.finance.pojo.ServiceManagement;
import com.gxa.finance.service.ServiceManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service

public class ServiceManagementServiceImpl extends ServiceImpl<ServiceManagementMapper, ServiceManagement> implements ServiceManagementService {
    @Autowired
    private ServiceManagementMapper serviceManagementMapper;

    @Override
    public R ServicePage(Integer currentPage, Integer pageSize) {
        Page<ServiceManagement> page = new Page(currentPage,pageSize);
        Page<ServiceManagement> pa = serviceManagementMapper.selectPage(page, null);
        R r = new R();
        try{
            if (pa!=null){
                r.setCode(200);
                r.setMsg("查询成功");
                r.setData(pa);
            }
        }catch(Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    //条件查询
    public R ifService(Page page,QueryWrapper<ServiceManagement> queryWrapper) {
//        Page<ServiceManagement> page = new Page(1,10);
//        QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
//                 //根据服务类型搜索服务推荐信息
//            if(StringUtils.isNotEmpty(serviceManagement.getServiceType())){
//            queryWrapper.eq("service_type",serviceManagement.getServiceType());
//                System.out.println("service====="+serviceManagement.getServiceType());
//           }
//            if (StringUtils.isNotEmpty(serviceManagement.getCountry())){
//                queryWrapper.eq("country",serviceManagement.getCountry());
//                System.out.println("service====="+serviceManagement.getCountry());
//            }
//            if(StringUtils.isNotEmpty(serviceManagement.getTime().toString())){
//                queryWrapper.in("time",serviceManagement.getTime());
//                System.out.println("service====="+serviceManagement.getTime());
//            }

            Page<ServiceManagement> page2 = serviceManagementMapper.selectPage(page,queryWrapper);
        R r = new R();
            if (page2!=null){
                 r.setMsg("查询成功");
                 r.setData(page2.getRecords());
                 r.setCode(200);
            }

        return r;
    }


    @Override
    //服务类型状态（启用）
    public R state(ServiceManagement serviceManagement){
        QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",serviceManagement.getId());
        //启用状态
        serviceManagement.setState(1);
        int i = serviceManagementMapper.update(serviceManagement, queryWrapper);
        R r = new R();
        if (i>0){
            r.setCode(200);
            r.setMsg("启用");
        }
        return r;
    }


    @Override
    //服务类型状态（禁用）
    public R noState(ServiceManagement serviceManagement){
        QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",serviceManagement.getId());
        //禁用状态
        serviceManagement.setState(0);
        int i = serviceManagementMapper.update(serviceManagement, queryWrapper);

        R r = new R();
        if (i>0){
            r.setCode(200);
            r.setMsg("禁用");
        }
        return r;
    }

    @Override
    //推荐服务 推荐（1/2）查询
        public  R manager(Integer currentPage,Integer pageSize) {
       Page<ServiceManagement> page = new Page(currentPage,pageSize);
       QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
       //状态为推荐
       queryWrapper.eq("recommend",1);
       //状态为启用
       queryWrapper.eq("state",1);
        Page<ServiceManagement> page1 = serviceManagementMapper.selectPage(page, queryWrapper);
        System.out.println("page1++++++++++++++++++++++++++++++++"+page1.getSize());
        R r = new R();
        if (page1!=null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(page1);
        }
        return r;
    }

    @Override
    //推荐服务 （已推荐/未推荐 状态接口）
    public R recommend(ServiceManagement serviceManagement) {
        QueryWrapper<ServiceManagement> que = new QueryWrapper<>();
        que.eq("id",serviceManagement.getId());
        serviceManagement.setRecommend(1);
        int i = serviceManagementMapper.update(serviceManagement, que);
        R r = new R();
        if (i>0){
            r.setCode(200);
            r.setMsg("已推荐");
        }
        return r;
    }

    @Override
    //推荐服务 （已推荐/未推荐 状态接口）
    public R noRecommend(ServiceManagement serviceManagement) {
        QueryWrapper<ServiceManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",serviceManagement.getId());
        serviceManagement.setRecommend(2);
        int i = serviceManagementMapper.update(serviceManagement, queryWrapper);
        R r = new R();
        if (i>0){
            r.setCode(200);
            r.setMsg("未推荐");
        }
        return r;
    }

    @Override
    public R open(String id, String status) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        ServiceManagement serviceManagement = new ServiceManagement();
        serviceManagement.setState(Integer.parseInt(status));
        serviceManagementMapper.update(serviceManagement,updateWrapper);
        R r=new R();
        return r;
    }

    @Override
    public R tui(String id, String statue) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        ServiceManagement serviceManagement = new ServiceManagement();
        serviceManagement.setState(Integer.parseInt(statue));
        serviceManagementMapper.update(serviceManagement,updateWrapper);
        R r = new R();
        return r;
    }


}
